package com.wxxymakers.grademark.controller;

import com.wxxymakers.grademark.model.entity.WxIncident;
import com.wxxymakers.grademark.model.entity.WxUser;
import com.wxxymakers.grademark.service.WxIncidentService;
import com.wxxymakers.grademark.utils.Const;
import com.wxxymakers.grademark.utils.RestResponse;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @AUTHOR soft
 * @DATE 2018/12/26 18:58
 * @DESCRIBE 评定事务控制器
 */
@RestController
@RequestMapping("/incident")
public class IncidentController {

    @Autowired
    private WxIncidentService incidentService;

    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @PostMapping("/add")
    public RestResponse add(WxIncident incident) {
        if (incidentService.add(incident)) {
            return RestResponse.ok(incident);
        }
        return RestResponse.fail();
    }

    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable int id) {
        if (incidentService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 获取进行中的事件
     */
    @PostMapping("/began")
    public RestResponse began() {
        WxIncident beganInc = incidentService.findBeganInc();
        if (beganInc != null) {
            return RestResponse.ok(beganInc);
        }
        return RestResponse.fail();
    }

    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @PostMapping("/change")
    public RestResponse change(WxIncident incident) {
        if (incidentService.change(incident)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/get/{id}")
    public RestResponse get(@PathVariable int id) {
        WxIncident incident = incidentService.findOne(id);
        if (incident != null) {
            return RestResponse.ok(incident);
        }
        return RestResponse.fail();
    }

    @PostMapping("/page")
    public RestResponse page(@RequestParam int page,
                             @RequestParam int limit,
                             WxIncident query,
                             @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        Integer collegeId = null;
        if (user.getRole().getSign().equals("college-admin")) {
            collegeId = user.getCollageId();
        }
        Page<WxIncident> incidents = incidentService.page(page, limit, query.getName(), query.getStatus(), collegeId);
        if (incidents != null) {
            return RestResponse.ok(incidents);
        }
        return RestResponse.fail();
    }

    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @PostMapping("/stop/{id}")
    public RestResponse stop(@PathVariable int id) {
        if (incidentService.stop(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @PostMapping("/restart/{id}")
    public RestResponse restart(@PathVariable int id) {
        if (incidentService.restart(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
